package com.one.group.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.service.impl.SpHouseRentalServiceImpl;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.*;
import com.one.group.model.vo.SpHouseRentalListVo;
import com.one.group.result.Result;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spHouseRental")
@Tag(name = "房屋租赁相关接口")
public class SpHouseRentalController {
    @Autowired
    private SpHouseRentalServiceImpl spHouseRentalService;
    @PostMapping("/add")
    @Operation(summary = "添加房屋租赁信息")
    public Result add(@RequestBody SpHouseRentalAddDto spHouseRentalAddDto){
        if(spHouseRentalService.mySave(spHouseRentalAddDto)){
            return ResultUtil.success("添加成功");
        }else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PutMapping("/update")
    @Operation(summary = "修改房屋租赁信息")
    public Result update(@RequestBody SpHouseRentalUpdateDto spHouseRentalUpdateDto){
        if(spHouseRentalService.myUpdate(spHouseRentalUpdateDto)){
            return ResultUtil.success("修改成功");
        }else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/rentHouse")
    @Operation(summary = "房屋出租")
    public Result rentHouse(@RequestBody SpHouseRentalRentDto spHouseRentalRentalRentDto){
        if (spHouseRentalService.rentHouse(spHouseRentalRentalRentDto)){
            return ResultUtil.success("出租成功");
        }else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/delete")
    @Operation(summary = "删除房屋租赁信息")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto ){
        if (spHouseRentalService.removeByIds(baseDeleteDto.getIds())){
            return ResultUtil.success();
        } else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/queryList")
    @Operation(summary = "查询房屋租赁信息")
    public Result queryList(@RequestBody SpHouseRentalQueryDto spHouseRentalQueryDto){
        PageHelper.startPage(spHouseRentalQueryDto.getPage(),spHouseRentalQueryDto.getLimit());
        List<SpHouseRentalListVo> spHouseRentalListVoList = spHouseRentalService.listSpHouseRentals(spHouseRentalQueryDto);
        PageInfo<SpHouseRentalListVo> pageInfo = new PageInfo<>(spHouseRentalListVoList);
        return ResultUtil.success(pageInfo);
    }
}
